package com.supor.aiot.food.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baseconfig.common.views.RoundImageView;
import com.bumptech.glide.Glide;
import com.supor.aiot.food.R;
import com.supor.aiot.food.api.SearchFoodImpl;
import com.supor.aiot.food.bean.RecipeTopicBean;
import com.supor.aiot.food.ui.food.SharedRecipeActivity;
import com.supor.aiot.food.utils.DoubleClickUtils;

import java.io.Serializable;
import java.util.List;


public class RecipeTopicAdapter extends RecyclerView.Adapter<RecipeTopicAdapter.TopicHolder>{
    private List<RecipeTopicBean> mList;
    private Context mContext;
    private static final String TOPIC_BEAN = "TOPIC_BEAN";

    public RecipeTopicAdapter(List<RecipeTopicBean> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecipeTopicAdapter.TopicHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_recipe_class,parent, false);
        return new RecipeTopicAdapter.TopicHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecipeTopicAdapter.TopicHolder holder, int position) {
        RecipeTopicBean topicBean = mList.get(position);
        holder.rlTopicOuter.setOnClickListener(view -> {
            if (DoubleClickUtils.isFastDoubleClick())return;
            Intent intent = new Intent(mContext, SharedRecipeActivity.class);
            intent.putExtra(TOPIC_BEAN, (Serializable) topicBean);
            mContext.startActivity(intent);

        });
        holder.tvTopicName.setText(topicBean.getCuisineTitel());
        Glide.with(mContext).load(topicBean.getDetailImage()).placeholder(new ColorDrawable(mContext.getResources().getColor(R.color.white_f5f5f5))).into(holder.rivTopic);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class TopicHolder extends RecyclerView.ViewHolder{
        private TextView tvTopicName;
        private RoundImageView rivTopic;
        private RelativeLayout rlTopicOuter;

        public TopicHolder(@NonNull View itemView) {
            super(itemView);
            tvTopicName =itemView.findViewById(R.id.tv_topic_name);
            rivTopic=itemView.findViewById(R.id.iv_recipe_class);
            rlTopicOuter=itemView.findViewById(R.id.rl_topic_outer);
        }
    }

}
